﻿using System;

namespace AviationSimulator.DataItems
{
    public class MathFunction
    {
        public MathFunctionName Name { get; set; }
        public double LowRange { get; set; }
        public double UpperRange { get; set; }
        public double Discrecity { get; set; }
        
        public int A { get; set; }
        public int B { get; set; }
        public int C { get; set; }

        private double _x;

        public MathFunction()
        {
            _x = 0;
        }

        public double GenerateParameterValue()
        {
            UpdateX();

            switch (Name)
            {
                case MathFunctionName.Linear:
                    return CalculateByNoneFuction();

                case MathFunctionName.Quedratic:
                    return CalculateByQuadraticFuction();

                case MathFunctionName.Exponential:
                    return CalculateByExponantialFuction();
                    
                case MathFunctionName.Ln:
                    return CalculateByLnFuction();

                case MathFunctionName.Sin:
                    return CalculateBySinFuction();
            }

            return 0;
        }

        private void UpdateX()
        {
            if (_x < LowRange || _x == UpperRange)
            {
                _x = LowRange;
                return;
            }

            _x = _x + Discrecity;

            if (_x > UpperRange)
                _x = UpperRange;
        }

        private double CalculateByNoneFuction()
        {
            return (A * _x + B);
        }

        private double CalculateByQuadraticFuction()
        {
            return (A * _x * _x + B * _x + C);
        }

        private double CalculateByExponantialFuction()
        {
            return Convert.ToInt32(Math.Pow(Math.E, _x));
        }

        private double CalculateByLnFuction()
        {
            return Convert.ToInt32(Math.Log(_x));
        }

        private double CalculateBySinFuction()
        {
            return Convert.ToInt32(Math.Sin(_x));
        }
    }
}
